const app = getApp()

function getAction(funcName,requestParam) {
  //初始化参数
  let method = 'GET';
  let domain = 'scf.tencentcloudapi.com/?';
  let Action = 'Invoke';
  let ClientContext = JSON.stringify(requestParam);
  //此处Nonce需要转化为字符串格式！！！
  let Nonce = (100 * Math.ceil(100 * Math.random(0, 1))).toString(); //随机正整数
  let Region = 'ap-guangzhou';
  let tencent_secretId = app.globalData.tencent_secretId;
  let tencent_secretKey = app.globalData.tencent_secretKey;
  let Timestamp = Math.floor(Date.now() / 1000); //当前时间戳
  let FunctionName = funcName;
  let Version = '2018-04-16';
  let url = '';

  //构造签名串
  let sign = method + domain + 'Action=' + Action + '&ClientContext=' + ClientContext + '&FunctionName=' + FunctionName + '&Nonce=' + Nonce + '&Region=' + Region + '&SecretId=' + tencent_secretId + '&Timestamp=' + Timestamp + '&Version=' + Version;
  const crypto = requirePlugin("crypto"); //crypto加密
  sign = new crypto.HmacSHA1(sign, tencent_secretKey).toString(crypto.Base64);
  sign = encodeURIComponent(sign);
  //console.log('sign = ' + sign);

  //构造请求url
  url = 'https://scf.tencentcloudapi.com/?' + 'Action=' + Action + '&ClientContext=' + ClientContext + '&FunctionName=' + FunctionName + '&Nonce=' + Nonce + '&Region=' + Region + '&SecretId=' + tencent_secretId + '&Signature=' + sign + '&Timestamp=' + Timestamp + '&Version=' + Version;
  //console.log('url = ' + url);

  return url;
}


module.exports = {
  getAction: getAction
}